<?php

function list_pr_yearly_income(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');

	$header = array(
		'Tahun'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Yearly Income'=>array('style'=>'border-bottom:2px solid;width:20%'),   
		'Periode Bayar'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Gaji Dasar Perhitungan'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'),   
		'Proses'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),   
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	$periode_id =get_periode_gaji();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$tanggal = $periode['tanggal'];

	$query = "SELECT * FROM pr_yearly_income 
		ORDER BY pr_yearly_income_id DESC";
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while( $ey =my_fetch_array($result) ){
		$detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=list_komponen&pr_yearly_income_id='.$ey['pr_yearly_income_id'],
			'title'=>'Komponen'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$detailproperty1 = array(
				'href'=>'index.php?com='.$_GET['com'].'&task=list_karyawan&pr_yearly_income_id='.$ey['pr_yearly_income_id'],
				'title'=>'Karyawan'
		);
		$detail_button1 = button_icon( 'b_usrlist.png' , $detailproperty1  );

		//if($ey['tanggal']>=$tanggal){
			$deleteproperty = array(
					'href'=>'javascript:; ',
					'onclick'=>'javascript:confirmDelete('.$ey['pr_yearly_income_id'].');',
					'title'=>'Delete'
				);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		//}else{
		//	$delete_button = button_icon( 'b_drop_off.png' , ''  );
		//}
		$yearly_incomes=my_get_data_by_id('yearly_income','yearly_income_id',$ey['yearly_income_id']) ;
		$periodes=my_get_data_by_id('pr_periode','periode_id',$ey['periode_id']) ;
		$row[] = array(
			'tahun'=>$ey['tahun'],
			'yearly_income'=>$yearly_incomes['label'],
			'bayar'=>date('m-Y',strtotime($ey['tanggal'])),
			'periode'=>date('m-Y',strtotime($periodes['tanggal'])),
			'proses'=>position_text_align($ey['proses'],'center'),
			' v'=>position_text_align($detail_button.' '.$detail_button1.' '.$delete_button,'right'),
		);
	}
	$datas = table_rows($row);
	$menuontop = array(  
		'Tambah data'=>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com=pr_yearly_income&task=edit\' });'),
	);
	$printmenu = additional_menu_on_list( $menuontop , '');
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu .table_builder($header , $datas ,  6  , false , $paging);
}

function form_pr_yearly_income(){
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);

	$view = form_header( "a" , "a"  );
	$month = date('m');
	$year = date('Y');
	
	$temp = array(
		'name'=>'tahun','size'=>5,
		'value'=>isset($_POST['tahun']) ? $_POST['tahun']: $year
	); 
	$temp_form = form_dynamic($temp );
	$view .= form_field_display( $temp_form , "Periode"    );	

	$datas = array();
	$query = "SELECT * FROM yearly_income ORDER BY label";
	$re = my_query($query);
	while($row = my_fetch_array($re) ){
		$datas[$row['yearly_income_id']] =$row['label'];
	}
	$temp = array(
		'name'=>'yearly_income_id',
		'id'=>'yearly_income_id',
		'value'=>isset($_POST['yearly_income_id']) ? $_POST['yearly_income_id'] : 0,
	);
	$form_temp = form_dropdown($temp , $datas);
	$view .= form_field_display( $form_temp , "Yearly Income" );
	
	$bulans  = array(
		'01'=>'01', 
		'02'=>'02', 
		'03'=>'03', 
		'04'=>'04', 
		'05'=>'05', 
		'06'=>'06', 
		'07'=>'07', 
		'08'=>'08', 
		'09'=>'09', 
		'10'=>'10', 
		'11'=>'11', 
		'12'=>'12', 
	);
	$temp = array(
		'name'=>'tanggal_bulan',
		'value'=>isset($_POST['tanggal_bulan']) ? $_POST['tanggal_bulan']: $month  
	);
	$bulan_form = form_dropdown( $temp,$bulans);
	
	$temp = array(
		'name'=>'tanggal_tahun','size'=>5,
		'value'=>isset($_POST['tanggal_tahun']) ? $_POST['tanggal_tahun']: $year
	); 
	$tahun_form = form_dynamic($temp );
	$view .= form_field_display( $bulan_form.' '. $tahun_form  , "Periode Bayar"    );

	$datas = array();
	$query = "SELECT * FROM pr_periode WHERE periode_id<".get_periode_gaji()." ORDER BY periode_id";
	$re = my_query($query);
	 while($row = my_fetch_array($re) ){
		$datas[$row['periode_id']] = date('m-Y',strtotime($row['tanggal']));
	 }
	$temp = array(
		'name'=>'periode_id',
		'id'=>'periode_id',
		'value'=>isset($_POST['periode_id']) ? $_POST['periode_id'] : $fields['periode_id'],
	);
	$form_temp = form_dropdown($temp , $datas);
	$view .= form_field_display( $form_temp , "Gaji Dasar Perhitungan" );
	
	$submit = array(
		'value' => ' Simpan ' ,
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function pr_yearly_income_validate(){
	$errsubmit = false;
	$err = array();

	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function submit_pr_yearly_income(){
	$tanggal = $_POST['tanggal_tahun'].'-'.$_POST['tanggal_bulan'].'-01';
	$datas = array(
		'tahun'=> my_type_data_str($_POST['tahun']),		
		'tanggal'=> my_type_data_str($tanggal),	
		'periode_id'=> my_type_data_int($_POST['periode_id']),
		'yearly_income_id'=> my_type_data_int($_POST['yearly_income_id']),
	);
	
	$id=my_insert_record( 'pr_yearly_income' , $datas );
	return update_pr_yearly_income_komponen($id) ;
	
}
 
function delete_pr_yearly_income($id){
	$query = "DELETE FROM pr_yearly_income_upload WHERE pr_yearly_income_id={$id}";
	my_query($query);	
	$query = "DELETE FROM pr_yearly_income_pajak WHERE pr_yearly_income_id ={$id} ";
	my_query($query);
	$query = "DELETE FROM pr_yearly_income_komponen WHERE pr_yearly_income_id ={$id} ";
	my_query($query);
	$query = "DELETE FROM pr_yearly_income_karyawan WHERE pr_yearly_income_id={$id}";
	my_query($query);	
	$query = "DELETE FROM pr_yearly_income_exception WHERE pr_yearly_income_id ={$id} ";
	my_query($query);
	$query = "DELETE FROM pr_yearly_income WHERE pr_yearly_income_id ={$id} ";
	return my_query($query);
}	


function list_komponen($pr_yearly_income_id){
	$pr_yearly_incomes=my_get_data_by_id('pr_yearly_income','pr_yearly_income_id',$pr_yearly_income_id) ;
	if($pr_yearly_incomes['proses']=="Y"){
		$header = array(
			'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
			'Komponen'=>array('style'=>'border-bottom:2px solid;width:30%'),  
			'Dasar Nilai'=>array('style'=>'border-bottom:2px solid;width:25%'),  
			'Pajak'=>array('style'=>'border-bottom:2px solid;width:10%'),  
			'Rupiah'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
			' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
		);
		$query = "
				SELECT * FROM pr_yearly_income_komponen a
				WHERE pr_yearly_income_id={$pr_yearly_income_id} 
				ORDER BY komponen_id
			";	
	}else{
		$header = array(
			'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
			'Komponen'=>array('style'=>'border-bottom:2px solid;width:20%'),  
			'Dasar Nilai'=>array('style'=>'border-bottom:2px solid;width:25%'),  
			'Pajak'=>array('style'=>'border-bottom:2px solid;width:10%'),  
			'Exception/Upload (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
			' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
		);
		$query = "
				SELECT * FROM pr_yearly_income_komponen a
				WHERE pr_yearly_income_id={$pr_yearly_income_id} 
				AND model_entry>0 AND model_entry<5 
				ORDER BY komponen_id
			";	
	}
	$result = my_query($query );
	$row = array(); 
	$i=0;
	while($ey = my_fetch_array($result)){
		if( $ey['model_entry']=='2' ){
			$temp=trim($ey['formula']);
			$komponen_gaji_id=(int) substr($temp,10,-1) ;
			$komponen_gajis=my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$komponen_gaji_id);
			$formula='['.$komponen_gajis['nama_komponen_gaji'].']';
		}else{	
			$formula=$ey['formula'];
		}		
		if($pr_yearly_incomes['proses']=="Y"){
			$detail_button = ' ';	
			if( $ey['tipe']=='Header' ){
				$rupiah=' ' ;
			}else{	
				$query = "
					SELECT SUM(nominal) as total FROM pr_yearly_income_karyawan 
					WHERE pr_yearly_income_id={$pr_yearly_income_id} 
					AND komponen_id=".$ey['komponen_id']
				;	
				$total_komponens = my_fetch_array(my_query($query )) ;
				$rupiah=rp_format($total_komponens['total']) ;
			}
			$row[] = array(
				'#'=> $ey['code']  ,   
				'komponen'=> $ey['label']  ,
				'formula'=> $formula  , 
				'pajak'=> $ey['kena_pajak']  , 
				'total_komponen'=>position_text_align( $rupiah , 'right' ), 
				' v'=>position_text_align($detail_button,'right'),
			);
		}else{	
			if( $ey['model_entry']=='4' ){
				$detailproperty = array(
					'href'=>'index.php?com='.$_GET['com'].'&task=list_upload&pr_yearly_income_id='.$pr_yearly_income_id.'&komponen_id='.$ey['komponen_id'],
					'title'=>'Upload'
				);
				$detail_button = button_icon( 'b_import.png' , $detailproperty  );
				$query = "
					SELECT SUM(nominal) as total FROM pr_yearly_income_upload
					WHERE pr_yearly_income_id={$pr_yearly_income_id} 
					AND komponen_id=".$ey['komponen_id']
				;	
			}else{ 
				$detailproperty = array(
					'href'=>'index.php?com='.$_GET['com'].'&task=list_exception&pr_yearly_income_id='.$pr_yearly_income_id.'&komponen_id='.$ey['komponen_id'],
					'title'=>'Exception'
				);
				$detail_button = button_icon( 'b_bookmark.png' , $detailproperty  );			
				$query = "
					SELECT SUM(nominal) as total FROM pr_yearly_income_exception
					WHERE pr_yearly_income_id={$pr_yearly_income_id} 
					AND komponen_id=".$ey['komponen_id']
				;	
			}
	
			$total_komponens = my_fetch_array(my_query($query )) ;
			$rupiah=rp_format($total_komponens['total']) ;
			$i++;
			$row[] = array(
				'#'=>position_text_align ($i, 'center'), 
				'komponen'=> $ey['label']  ,
				'formula'=> $formula  , 
				'pajak'=> $ey['kena_pajak']  , 
				'total_komponen'=>position_text_align( $rupiah , 'right' ), 
				' v'=>position_text_align($detail_button,'right'),
			);			
		}	
	}
	
	$yearly_incomes = my_get_data_by_id('yearly_income','yearly_income_id',$pr_yearly_incomes['yearly_income_id'] );
	$view  = form_header( "..." , "..."  );
	$view .=form_field_display( '<span class="label_form">'.  $pr_yearly_incomes['tahun'] .'</span>'   , "<b>Tahun</b>"    ) ;
	$view .=form_field_display( '<span class="label_form">'.  $yearly_incomes['label'].'</span>'   , "<b>Yearly Income</b>"    ) ;
	$view .=form_field_display( '<span class="label_form">'.  date( "m-Y" , strtotime($pr_yearly_incomes['tanggal'])) .'</span>'   , "<b>Periode Bayar</b>"    ) ;
	$periodes = my_get_data_by_id('pr_periode','periode_id',$pr_yearly_incomes['periode_id']);
	$view .=form_field_display( '<span class="label_form">'.  date( "m-Y" , strtotime($periodes['tanggal'])) .'</span>'   , "<b>Gaji Dasar Perhitungan</b>"    ) ;
	
	$view  .= form_footer(  );
	if($pr_yearly_incomes['proses']=="Y"){
		$menuontop = array(  
			'Kembali' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_yearly_income\''), 
			'Unproses' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_yearly_income&task=unproses&pr_yearly_income_id='.$pr_yearly_income_id.'\''),
			'List Karyawan' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_yearly_income&task=list_karyawan&pr_yearly_income_id='.$pr_yearly_income_id.'\''),
		);	
	}else{
		$menuontop = array(  
			'Kembali' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_yearly_income'.'\''), 
			'Proses' => array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com=pr_yearly_income&task=loadpage&pr_yearly_income_id='.$pr_yearly_income_id.'\' });'),
		);	
	}
	$printmenu = additional_menu_on_list( $menuontop ,'' ); 
	$datas = table_rows($row);  
	return $view.$printmenu.table_builder($header , $datas , 4, false ,$paging);
}

function update_pr_yearly_income_karyawan($pr_yearly_income_id){

	ini_set("max_execution_time","10000");	
	$query = "DELETE FROM pr_yearly_income_pajak 
		WHERE pr_yearly_income_id={$pr_yearly_income_id}";
	$result = my_query($query); //reset				
	$query = "DELETE FROM pr_yearly_income_karyawan WHERE pr_yearly_income_id={$pr_yearly_income_id}";
	my_query($query);
	$pr_yearly_incomes=my_get_data_by_id('pr_yearly_income','pr_yearly_income_id',$pr_yearly_income_id) ;
	$tanggal=$pr_yearly_incomes['tanggal'] ;

	$bulan = (int) date('m' , strtotime($tanggal));
	$tahun = (int) date('Y' , strtotime($tanggal));
	if($bulan<=3){
		$bulan_kontrak=12+$bulan-3 ;
		$tahun_kontrak=$tahun-1 ;
	}else{	
		$bulan_kontrak=$bulan-3 ;
		$tahun_kontrak=$tahun ;
	}
	$tgl_kontrak=$tahun_kontrak.'-'.sprintf( '%02d',$bulan_kontrak) .'-01';	
	if($bulan =='01' ){
		$bulan = '12';
		$tahun-- ;	
	}else{
		$bulan-- ;
	}			
	$tgl=$tahun.'-'.sprintf( '%02d',$bulan) .'-11';
	
	$query = "
		SELECT * FROM karyawan a 
		INNER JOIN kekaryawanan b ON b.karyawan_id=a.karyawan_id 
		WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
		AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  ) 
		" ;
	$loop1=true ;
	$komponen_pajak=$komponen_pembulatan=$komponen_total=0;
	$result = my_query($query);
	while($ey = my_fetch_array($result)){
		$query = "
				SELECT * FROM pr_yearly_income_komponen 
				WHERE pr_yearly_income_id={$pr_yearly_income_id} 
				ORDER BY komponen_id
			";	
		$res_komponen = my_query($query );
		$total_nominal=0 ;
		$rp_komponen_pajak1=$rp_komponen_pajak2=array();
		$hitung_pajak=false;
		while($ey_komponen = my_fetch_array($res_komponen)){
			$nominal = 0;
			if($loop1){
				if($ey_komponen['model_entry']==3 ){
					$komponen_pajak=$ey_komponen['komponen_id'] ;
				}
				if($ey_komponen['model_entry']==5 ){
					$komponen_pembulatan=$ey_komponen['komponen_id'] ;
				}
				if($ey_komponen['model_entry']==6 ){
					$komponen_total=$ey_komponen['komponen_id'] ;
				}
			}	
			if($ey_komponen['tipe']=='Header' ){
			}elseif($ey_komponen['tipe']=='Sub Total' ){ 
			}elseif($ey_komponen['tipe']=='Detil' ){
				//CARI DI EXCEPTION, if ada pake exception 
				if($ey_komponen['model_entry']==4 ){ //upload
					$query = "
						SELECT nominal FROM pr_yearly_income_upload 
						WHERE pr_yearly_income_id={$pr_yearly_income_id} 
						AND komponen_id ={$ey_komponen['komponen_id']} 
						AND karyawan_id ={$ey['karyawan_id']}
					";
					$dres = my_query($query);
					$drow = my_fetch_array($dres);
					$nominal = $drow['nominal'];
					
							if($ey_komponen['komponen_id'] == '7'){var_dump(3);}
				}else{
					$query = "
						SELECT nominal FROM pr_yearly_income_exception 
						WHERE pr_yearly_income_id={$pr_yearly_income_id} 
						AND komponen_id ={$ey_komponen['komponen_id']} 
						AND karyawan_id ={$ey['karyawan_id']}
					";
					$dres = my_query($query);
					if( my_num_rows($dres) > 0){
						$drow = my_fetch_array($dres);
						$nominal = $drow['nominal']; 
					}else{	 
						$trr=get_formula($ey_komponen['formula'], $ey['karyawan_id'], $pr_yearly_incomes['periode_id'] );  
						if((int)  trim($trr)  <> 0){			
							eval("\$nominal  =  ".  $trr .";" ) ; 	 
						}	
					}
				}	
			}
			if($ey_komponen['kena_pajak']=="Y"){
				$rp_komponen_pajak1[$ey_komponen['komponen_pajak_1']]+=$nominal ;
				$rp_komponen_pajak2[$ey_komponen['komponen_pajak_2']]+=$nominal ;
				$hitung_pajak=true;
			}	
			$total_nominal+=$nominal ;

			$datas = array(
				'pr_yearly_income_id'=>my_type_data_int($pr_yearly_income_id),
				'karyawan_id'=>my_type_data_int($ey['karyawan_id']),
				'komponen_id'=>my_type_data_int($ey_komponen['komponen_id']),
				'nominal'=>my_type_data_str( $nominal ),
			);
			  
			my_insert_record('pr_yearly_income_karyawan',$datas);
		}
		$loop1=false ;
		
		//hitung pajak
		if( $hitung_pajak ){
			$pajak=get_pajak( $rp_komponen_pajak1, $rp_komponen_pajak2, $ey['karyawan_id'], $pr_yearly_incomes['periode_id'], $pr_yearly_income_id  );
		}else{
			$pajak=0;
		}
		
		$selisih_pembulatan=($total_nominal-floor($total_nominal/100)*100)*-1 ;
		$total_nominal+=$selisih_pembulatan;
		if( $komponen_pembulatan > 0){
			$query = "UPDATE pr_yearly_income_karyawan SET 
				nominal = '".$selisih_pembulatan."' 
				WHERE karyawan_id=".$ey['karyawan_id']." 
				AND pr_yearly_income_id=".$pr_yearly_income_id." 
				AND komponen_id=".$komponen_pembulatan ;
			//my_query($query);
		}	
		if( $komponen_total > 0){
			$query = "UPDATE pr_yearly_income_karyawan SET 
				nominal = '".$total_nominal."' 
				WHERE karyawan_id=".$ey['karyawan_id']." 
				AND pr_yearly_income_id=".$pr_yearly_income_id." 
				AND komponen_id=".$komponen_total ;
			my_query($query);
		}	
		if( $komponen_pajak > 0){
			$query = "UPDATE pr_yearly_income_karyawan SET 
				nominal = '".$pajak."' 
				WHERE karyawan_id=".$ey['karyawan_id']." 
				AND pr_yearly_income_id=".$pr_yearly_income_id." 
				AND komponen_id=".$komponen_pajak ;
			my_query($query);
		}	

		//update sub total
		$query = "SELECT a.komponen_id,b.tipe,a.nominal FROM pr_yearly_income_karyawan a
				INNER JOIN pr_yearly_income_komponen b ON b.komponen_id=a.komponen_id 
				AND b.pr_yearly_income_id=".$pr_yearly_income_id."  
				WHERE a.karyawan_id=".$ey['karyawan_id']." 
				AND a.pr_yearly_income_id=".$pr_yearly_income_id." 
				ORDER BY a.komponen_id" ;
		$res_subtotal=my_query($query);	
		$substotal=0 ;		
		while($ey_subtotal = my_fetch_array($res_subtotal)){
			if( $ey_subtotal['tipe']=='Sub Total' ){
				$query = "UPDATE pr_yearly_income_karyawan SET 
					nominal = '".$substotal."' 
					WHERE karyawan_id=".$ey['karyawan_id']." 
					AND pr_yearly_income_id=".$pr_yearly_income_id." 
					AND komponen_id=".$ey_subtotal['komponen_id'] ;
				my_query($query);		
				$substotal=0 ;		
			}elseif( $ey_subtotal['tipe']=='Header' ){
				$substotal=0 ;	
			}else{
				$substotal+= $ey_subtotal['nominal'] ;
			}
		}
		set_takehome_pay($ey['karyawan_id'] , $pr_yearly_income_id);
	}

	$query = "UPDATE pr_yearly_income SET 
			proses = 'Y' 
			WHERE pr_yearly_income_id=".$pr_yearly_income_id ;
		my_query($query);
			
	return true;
	
}

function list_karyawan($pr_yearly_income_id){
	$query = "
		SELECT komponen_id FROM pr_yearly_income_komponen 
		WHERE pr_yearly_income_id={$pr_yearly_income_id} AND model_entry=6 
		LIMIT 1
	";
	$komponens= my_fetch_array(my_query($query )) ;
	$komponen_total=$komponens['komponen_id'] ;
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:50%'),  
		'total'=>array('style'=>'text-align:right;border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 		
	);
	if(isset($_GET['key'])){
		$query = "
			SELECT * FROM pr_yearly_income_karyawan a
			INNER JOIN karyawan b ON a.karyawan_id=b.karyawan_id 
			INNER JOIN biodata c ON c.biodata_id=b.biodata_id 
			WHERE a.pr_yearly_income_id={$pr_yearly_income_id} 
			AND a.komponen_id={$komponen_total} 
			AND ( c.nama LIKE '%{$_GET['key']}%' OR b.nik ='{$_GET['key']}' )
			ORDER BY b.nik 
		";
	}else{
		$query = "
			SELECT * FROM pr_yearly_income_karyawan a
			INNER JOIN karyawan b ON a.karyawan_id=b.karyawan_id 
			WHERE a.pr_yearly_income_id={$pr_yearly_income_id} 
			AND a.komponen_id={$komponen_total} ORDER BY b.nik 
		";
	}
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&pr_yearly_income_id={$_GET['pr_yearly_income_id']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25;  
	while($ey = my_fetch_array($result)){
		$detailproperty = array(
				'href'=>'index.php?com='.$_GET['com'].'
					&task=list_karyawan_komponen
					&pr_yearly_income_id='.$ey['pr_yearly_income_id'].'
					&karyawan_id='.$ey['karyawan_id'],
				'title'=>'Komponen per Karyawan'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$i++;
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 
 		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>  $karyawan['nik'] ,   
			'nama'=>  $karyawan['nama_gelar'] ,   
			'total'=> position_text_align (rp_format($ey['nominal']),'right')  ,   
			'operasi'=> position_text_align( $detail_button, 'right'),
		);
	}
	$search = '<form method="GET">
	<input type="hidden" name="com" value="'.$_GET['com'].'" />
	<input type="hidden" name="task" value="'.$_GET['task'].'" />
	<input type="hidden" name="pr_yearly_income_id" value="'.$_GET['pr_yearly_income_id'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" /><input class="simple_search" type="submit" value=" CARI " /> </form>';
	$pr_yearly_incomes = my_get_data_by_id('pr_yearly_income','pr_yearly_income_id',$pr_yearly_income_id);
	$yearly_incomes = my_get_data_by_id('yearly_income','yearly_income_id',$pr_yearly_incomes['yearly_income_id'] );
	$view ='<table width="98%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= '<table width="100%"   style="border-collapse:collapse;border-color:white" cellspacing="0" cellpadding="2">';
	$view .= form_field_display( '<span class="label_form">'.  date( "m-Y" , strtotime($pr_yearly_incomes['tanggal'])) .'</span>'   , "<b>Periode</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.  $yearly_incomes['label'].'</span>'   , "<b>Yearly Income</b>"    ); 
	$view .= '<tr><td>'.$search .'</td><td valign="bottom"><div style="width:100%;text-align:right;">
	<input type="button" value=" Kembali" class="main_button" onclick="javascript:location.href=\'index.php?com=pr_yearly_income\'"/>
	<input type="button" value=" Excel" class="main_button" onclick="javascript:location.href=\'index.php?com=pr_yearly_income&task=excel&pr_yearly_income_id='.$pr_yearly_income_id.'\'"/>
	<input type="button" value=" List Komponen" class="main_button" onclick="javascript:location.href=\'index.php?com=pr_yearly_income&task=list_komponen&pr_yearly_income_id='.$pr_yearly_income_id.'\'"/>
	</div></td></tr>'   ; 
	$view .= '</table>'; 
	$view .= '</td></tr></table><br/>';
	
	$datas = table_rows($row);   
	$paging = $kgPagerOBJ ->showPaging();
	return  $view . table_builder($header , $datas , 4, false ,$paging);
}
 

function excel_report_old($pr_yearly_income_id){
	my_component_load('xl_builder' , false);
	$query = "SELECT * FROM pr_yearly_income_komponen  
			WHERE pr_yearly_income_id={$pr_yearly_income_id} 
			ORDER BY komponen_id "
	;
	$res_komponen = my_query($query);
	$temp['NIK']=array();
	$temp['Nama']=array();
	while($ey = my_fetch_array($res_komponen)){
		$temp[$ey['label']]=array();
	}
	$header = $temp;

	$query = "SELECT * FROM pr_yearly_income_karyawan
				WHERE pr_yearly_income_id={$pr_yearly_income_id} 
				ORDER BY karyawan_id, komponen_id	
			";	
	$result = my_query($query);
	$karyawan_id=0;
	$row =array();
	while($ey = my_fetch_array($result)){
		if( $ey['karyawan_id'] <> $karyawan_id ){
			if ( $karyawan_id <> 0){
				$row[] = $det;
			}	
			$karyawan_id=$ey['karyawan_id'] ;
			$karyawan = loaddata_karyawan($ey['karyawan_id']);
			$det = array();
			$det[0]=position_text_align ($karyawan['nik'] ,'center');
			$det[1]=$karyawan['nama_gelar'] ;
		}	
		$det[$ey['komponen_id']+1]= $ey['nominal'] ;
	}

	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,  24 ,false );
}



function excel_report($pr_yearly_income_id){
	my_component_load('xl_builder' , false);
	$query = "SELECT * FROM pr_yearly_income_komponen  
			WHERE pr_yearly_income_id={$pr_yearly_income_id} 
			ORDER BY komponen_id ";
	$res_komponen = my_query($query);
	$temp['NIK']=array();
	$temp['Nama']=array();
	while($ey = my_fetch_array($res_komponen)){
		$temp[$ey['label']]=array();
	}
	$header = $temp;
	$query = "SELECT * FROM pr_yearly_income_komponen  
			WHERE pr_yearly_income_id={$pr_yearly_income_id} 
			ORDER BY komponen_id ";
	$res_komponen = my_query($query);
	$query = "			
	SELECT * FROM karyawan b
	INNER JOIN pr_yearly_income_karyawan a ON a.karyawan_id=b.karyawan_id 
	WHERE a.pr_yearly_income_id= {$pr_yearly_income_id} 
	GROUP BY b.karyawan_id
	ORDER BY b.nik 
		"; 
	$result = my_query($query );
	$row = array();
	while( $ey = my_fetch_array($result) ){
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		unset($det);
		$det = array();
		$det[0]=$karyawan['nik']  ;
		$det[100]=$karyawan['nama_gelar'] ; 
		$query = "SELECT * FROM pr_yearly_income_komponen  
				WHERE pr_yearly_income_id={$pr_yearly_income_id} 
				ORDER BY komponen_id ";
		$res_komponen = my_query($query);
		while( $eycom = my_fetch_array($res_komponen) ){ 
			$det[$eycom['komponen_id']] = get_komponen_data($karyawan['karyawan_id'] , $eycom['komponen_id'] , $pr_yearly_income_id);
			
		}
		$row[]=$det; 
	}
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,  24 ,false );
}
function list_karyawan_komponen($pr_yearly_income_id, $karyawan_id){
	$karyawan = loaddata_karyawan($karyawan_id);

	$header = array(
		'#'=>array('style'=>'text-align:right;border-bottom:2px solid;width:5%'),  
		'Komponen'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Rupiah'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
		' '=>array('style'=>'text-align:right;border-bottom:2px solid;width:55%'),  
	);
	$pr_yearly_incomes=my_get_data_by_id('pr_yearly_income','pr_yearly_income_id',$pr_yearly_income_id) ;
	$query = "
			SELECT * FROM pr_yearly_income_karyawan a
			INNER JOIN pr_yearly_income_komponen b ON b.komponen_id=a.komponen_id  
			AND b.pr_yearly_income_id=a.pr_yearly_income_id
			WHERE a.pr_yearly_income_id={$pr_yearly_income_id} 
			AND a.karyawan_id={$karyawan_id} 
			ORDER BY a.komponen_id
		";	

	$result = my_query($query );
	$row = array(); 
	while($ey = my_fetch_array($result)){
		if(  $ey['tipe']=='Header'){
			$nominal=' ' ;
		}else{
			$nominal=rp_format( $ey['nominal']  );
			if(  $ey['kategori']=='-'){			
				$nominal*=-1 ;
			}	
		}	
		$row[] = array(
			'#'=>  position_text_align ($ey['code'] ,'right'),      
			'komponen'=> $ey['label']  ,
			'nominal'=>  position_text_align ( $nominal ,'right'),      
			'ops'=> ' '  ,
		);
	}

	$yearly_incomes = my_get_data_by_id('yearly_income','yearly_income_id',$pr_yearly_incomes['yearly_income_id'] );
	$newline=array(
		'Yearly Income'=>$yearly_incomes['label'],
		'Periode'=>date( "m-Y" , strtotime($pr_yearly_incomes['tanggal']))
	);		
	$pro =   karyawan_header_detail($karyawan,true, $newline  );
	$menuontop = array(  
		'Pajak' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_yearly_income&task=list_karyawan_pajak&pr_yearly_income_id='.$pr_yearly_income_id.'&karyawan_id='.$karyawan_id.'\''), 
		'Kembali' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_yearly_income&task=list_karyawan&pr_yearly_income_id='.$pr_yearly_income_id.'\''), 
	);	
	$datas = table_rows($row);  
	$printmenu = additional_menu_on_list( $menuontop , '');
	return  $pro.$printmenu.table_builder($header , $datas ,  6 , false   );	
}


function page_kalkulasi_pr_yearly_income(){
	my_set_code_js('
	$(document).ready(function() {
       	$(\'#onebar\').load(\'index.php?com='.$_GET['com'].'&task=proses&pr_yearly_income_id='.$_GET['pr_yearly_income_id'].'&'.rand(0,9).'='.rand(0,99999).'\'); 
	});
	');
	return '<div id="onebar" style="width:100%;text-align:center;"><img src="components/system/jquery/facebox/loading.gif" /><br/>On processing!!!</div>';
}
	
function update_pr_yearly_income_komponen($id){

	$pr_yearly_incomes=my_get_data_by_id('pr_yearly_income','pr_yearly_income_id',$id) ;
	$query = "
			SELECT b.code,b.label as uraian_judul,b.uraian_total,b.kategori,c.*   		
			FROM yearly_income a
			INNER JOIN yearly_group b ON b.yearly_income_id=a.yearly_income_id 
			INNER JOIN yearly_komponen c ON c.yearly_group_id=b.yearly_group_id 
			WHERE a.yearly_income_id={$pr_yearly_incomes['yearly_income_id']} 
			ORDER BY b.code, c.yearly_komponen_id
		";
	$result = my_query($query );
	
	$no=0;
	$code=$kategori=$uraian_total="";
	while($ey = my_fetch_array($result)){

		if( $ey['model_entry']==1 ){ //Gaji Pokok
			$formula='[GAPOK]' ;
		}elseif( $ey['model_entry']==2 ){ //Komponen Gaji
			$formula='[KOMPONEN_'.$ey['komponen_gaji_id'].']' ;			
		}elseif( $ey['model_entry']==3 ){ //Nilai Pajak
			$formula='[PAJAK]' ;		
		}elseif( $ey['model_entry']==4 ){ //Upload
			$formula='[UPLOAD]' ;
		}elseif( $ey['model_entry']==5 ){ //Selisih Pembulatan
			$formula='[SELISIH]' ;
		}elseif( $ey['model_entry']==6 ){ //Total
			$formula='[TOTAL]' ;	
		}	
		$formula.=$ey['formula'] ;
		
		if($code <>  $ey['code']){
			if($code <> " "){
				if($uraian_total<>''){ 
					$no++;
					$datas = array(
						'pr_yearly_income_id'=>my_type_data_int($id),
						'komponen_id'=>my_type_data_int($no),
						'code'=>my_type_data_str(' '), 
						'label'=>my_type_data_str($uraian_total), 
						'tipe'=>my_type_data_str('Sub Total'), 
						'formula'=>my_type_data_str(' '), 
						'model_entry'=>my_type_data_int(0),
						'kategori'=> my_type_data_str($kategori),
					);
					my_insert_record('pr_yearly_income_komponen' , $datas);
				}	
			}
			$uraian_total= $ey['uraian_total'] ;
			if( $ey['nama_komponen'] =='' ){
				$no++;
				$datas = array(
					'pr_yearly_income_id'=>my_type_data_int($id),
					'komponen_id'=>my_type_data_int($no),
					'code'=>my_type_data_str(  $ey['code'] ), 
					'label'=>my_type_data_str($ey['uraian_judul']), 
					'tipe'=>my_type_data_str('Detil'), 
					'formula'=>my_type_data_str($formula), 
					'model_entry'=>my_type_data_int($ey['model_entry']),
					'kena_pajak'=> my_type_data_str($ey['kena_pajak']),
					'komponen_pajak_1'=> my_type_data_int($ey['komponen_pajak_1']),
					'komponen_pajak_2'=> my_type_data_int($ey['komponen_pajak_2']),
					'kategori'=> my_type_data_str($kategori),
				);
				my_insert_record('pr_yearly_income_komponen' , $datas);		

			}else{
				$no++;
				$datas = array(
					'pr_yearly_income_id'=>my_type_data_int($id),
					'komponen_id'=>my_type_data_int($no),
					'code'=>my_type_data_str( $ey['code'] ), 
					'label'=>my_type_data_str($ey['uraian_judul']), 
					'tipe'=>my_type_data_str('Header'), 
					'formula'=>my_type_data_str(''), 
					'model_entry'=>my_type_data_int(0),
				);
				my_insert_record('pr_yearly_income_komponen' , $datas);		
			}
			$code=$ey['code'] ;
			$kategori=$ey['kategori'] ;
		}
		if( $ey['nama_komponen'] <>'' ){
			$no++;
			$datas = array(
				'pr_yearly_income_id'=>my_type_data_int($id),
				'komponen_id'=>my_type_data_int($no),
				'code'=>my_type_data_str( '' ), 
				'label'=>my_type_data_str($ey['nama_komponen'] ), 
				'tipe'=>my_type_data_str('Detil'), 
				'formula'=>my_type_data_str($formula), 
				'model_entry'=>my_type_data_int($ey['model_entry']),
				'kena_pajak'=> my_type_data_str($ey['kena_pajak']),
				'komponen_pajak_1'=> my_type_data_int($ey['komponen_pajak_1']),
				'komponen_pajak_2'=> my_type_data_int($ey['komponen_pajak_2']),
				'kategori'=> my_type_data_str($kategori),
			);
			my_insert_record('pr_yearly_income_komponen' , $datas);			
		}
		
	}
	if($uraian_total<>''){ 
		//'Sub Total','Total'	
			$no++;
			$datas = array(
				'pr_yearly_income_id'=>my_type_data_int($id),
				'komponen_id'=>my_type_data_int($no),
				'code'=>my_type_data_str( '' ), 
				'label'=>my_type_data_str( $uraian_total  ), 
				'tipe'=>my_type_data_str('Total'), 
				'formula'=>my_type_data_str(''), 
				'model_entry'=>my_type_data_int(0),
				'kategori'=> my_type_data_str($kategori),
			);
			my_insert_record('pr_yearly_income_komponen' , $datas);		
	}		
	return true ;
}	
	

function jumlah_hari_kerja($tmb){
	 
	$startTimeStamp = strtotime($tmb);
	$endTimeStamp = strtotime(date('Y')."-12-31");

	$timeDiff = abs($endTimeStamp - $startTimeStamp);

	$numberDays = $timeDiff/86400;  // 86400 seconds in one day

	// and you might want to convert to integer
	return intval($numberDays);
}		

function get_gaji_dibayar_hitung($gapok,$tmb){
	$gaji_dibayar=$gapok ;
	if(date('Y' , strtotime($tmb) ) < date('Y') )return $gapok;
	$hari = jumlah_hari_kerja($tmb);
	$nilai = $gapok * $hari / 365; 
	return $nilai;
}


function komponen_status($komponens , $karyawan_status_id){
 
	if($komponens['kontrak'] =='yes'){
		if( (int) $karyawan_status_id == 2)return true;
	}
	if($komponens['organik'] =='yes'){
		if( (int) $karyawan_status_id == 1 )return true;
	}
	if($komponens['perbantuan'] =='yes'){
		if( (int) $karyawan_status_id == 3)return true; 
	}
	if($komponens['alih_status'] =='yes'){
		if( (int) $karyawan_status_id == 4)return true;
	}
	return false;
}
   
function get_formula( $str , $karyawan_id, $periode_id ){
	$formulas ='' ;
	$ntext=0;
	$text = $str;
	$karyawan = loaddata_karyawan($karyawan_id); 
	$karyawan_status_id = $karyawan['status_kekaryawan_id'];
 
	while( strlen($text) >0 ){ 
		$pos1 = strpos($text, '[');
		$pos2 = strpos($text, ']');
		if($pos1 === false){
			$formulas.=$text ;
			$ntext=strlen($text) ;
		}else{
			if($pos1>0){
				$ntext=$pos1 ;
				$formulas.=substr($text,0,$ntext) ;
			}else{
				$ntext=$pos2+1 ;
				$komponen=substr($text,0,$ntext) ;
				if($komponen=='[GAPOK]'){
					$gaji_dibayar=get_gaji_dibayar_hitung( $karyawan['gapok_km'],$karyawan['tmb_perumahan']);	
					$formulas.=   $gaji_dibayar;
/*					
				}elseif($komponen=='[UPLOAD]'){
					$query  = "SELECT gapok_km FROM pr_gaji WHERE karyawan_id = {$karyawan_id} and periode_id={$periode_id}";
					$temp = my_fetch_array(my_query($query));
					$formulas.=  isset( $temp['gapok_km'] ) ? $temp['gapok_km'] :  0;					
*/				
				}elseif(substr($komponen,1,9)=='KOMPONEN_'){	
					$komponen_id=(int) substr($komponen,10,-1) ;
					 $query  = "SELECT kalkulasi FROM pr_gaji_komponen 
						WHERE karyawan_id = {$karyawan_id} 
							and periode_id={$periode_id} 
							and komponen_id={$komponen_id}";
					$temp = my_fetch_array(my_query($query));
					if(! isset( $temp['kalkulasi'] ) ){
						$komponens = my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$komponen_id);
						$test_status = komponen_status($komponens , $karyawan_status_id); 
						if($test_status){
							my_component_load('pr_formula_gaji' , false); 
							my_component_load('pr_adjustment'  , false ); 
							if($komponens['model_entry'] == '2'){ //rumus
								$trr=formula_umum($komponens['formula'], $karyawan_id , 0);  
								if((int)strlen(trim($trr)) <> 0){			
									eval("\$tghx  =  (int)".  $trr .";" ) ; 
									if((int)$komponens['tipe'] == 2){
										$formulas.= (-1) * $tghx;
									}else{
										$formulas.=   $tghx;
									}
								} 
							} 
						} 	
					}else{
						$formulas.= isset( $temp['kalkulasi'] ) ?$temp['kalkulasi'] :  0; 
					}
				}else{
					$formulas.='0' ;
				}	
			}
		}
		$text=substr($text,$ntext) ;
	} 
	return $formulas ;
}	

function list_upload($pr_yearly_income_id, $komponen_id){
	my_set_code_js('
		function resetConfirm(){
			var t = confirm(\'Data akan direset?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=upload_reset&pr_yearly_income_id='.$_GET['pr_yearly_income_id'].'&komponen_id='.$_GET['komponen_id'].'\';
			}
			return false;
		}
			
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=upload_delete&pr_yearly_income_id='.$_GET['pr_yearly_income_id'].'&komponen_id='.$_GET['komponen_id'].'&karyawan_id=\'+id;
			}
			return false;
		}
	');
	$header = array(
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Nama'=>array('style'=>'text-align:left;border-bottom:2px solid;width:65%'),  
		'Nominal'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	if(isset($_GET['key']))
		$query = "SELECT a.karyawan_id , a.nominal FROM pr_yearly_income_upload a
			INNER JOIN karyawan c  ON a.karyawan_id = c.karyawan_id
			INNER JOIN biodata d ON d.biodata_id = c.biodata_id
			WHERE a.pr_yearly_income_id={ $pr_yearly_income_id } 
			AND a.komponen_id={ $komponen_id } 
			AND ( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' )
			ORDER BY c.NIK
			";
	else
		$query = " SELECT a.karyawan_id , a.nominal FROM pr_yearly_income_upload a
			INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id 
			WHERE a.pr_yearly_income_id=".$pr_yearly_income_id."
			AND a.komponen_id=".$komponen_id 
		;
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =7;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		  
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&pr_yearly_income_id={$_GET['pr_yearly_income_id']}&komponen_id={$_GET['komponen_id']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$karyawan = loaddata_karyawan($ey['karyawan_id']);

		$editproperty = array(
			'href'=>'index.php?com=pr_yearly_income&task=edit_upload
				&pr_yearly_income_id='.$pr_yearly_income_id.'
				&komponen_id='.$komponen_id.'
				&karyawan_id='.$ey['karyawan_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		 
		$deleteproperty = array(
				'href'=>'javascript:; ',
				'onclick'=>'javascript:confirmDelete('.$ey['karyawan_id'].');',
				'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		
		$row[] = array(
			'nik'=>position_text_align ($karyawan['nik'] ,'center'),    
			'nama'=> $karyawan['nama_gelar'] ,      
			'nominal'=>position_text_align('Rp. '.rp_format($ey['nominal']) , 'right')  ,
			'operasi'=> position_text_align( $edit_button .' '.$delete_button, 'right'),
		); 
	}
	$datas = table_rows($row);
	$paging = $kgPagerOBJ ->showPaging();	

	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .='<table width="100%"   style="border-collapse:collapse;border-color:white" cellspacing="0" cellpadding="2">';
	$view .= form_field_display( '<span class="label_form"><b>'.get_komponen($pr_yearly_income_id,$komponen_id).'</b></span>', "<b>Komponen</b>"    ); 
	$view .= '<tr><td><br/>'.$search .'</td><td valign="bottom"><div style="width:100%;text-align:right;">
	<input type="button" value="Tambah Data" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com=pr_yearly_income&task=edit_upload&&pr_yearly_income_id='.$pr_yearly_income_id.'&komponen_id='.$komponen_id.'&karyawan_id=0\' });" class="main_button" /> 
	<input type="button" value="Reset Data" onclick="javascript:resetConfirm()" class="main_button" /> 
	<input type="button" value="Upload" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com=pr_yearly_income&task=upload&pr_yearly_income_id='.$pr_yearly_income_id.'&komponen_id='.$komponen_id.'\' });" class="main_button" />  
	<input type="button" value="Kembali" class="main_button" onclick="javascript:location.href=\'index.php?com=pr_yearly_income&task=list_komponen&pr_yearly_income_id='.$pr_yearly_income_id.'\'"/></div></td></tr>'   ; 
	return  $view.table_builder($header , $datas ,  6 , false ,$paging );	

}


function upload_form_page($pr_yearly_income_id, $komponen_id){
	$view = form_header( "upload" , "upload"  );
	
	$file = array(
			'name'=>'file',
			'value'=>(isset($_POST['file'])? $_POST['file'] : $fields['file']),
			'id'=>'file' ,'size'=>'70',
			'type'=>'file'
	);
	$form_nominal = form_dynamic($file);
	$view .= form_field_display( $form_nominal , "File CSV" );
	
	$submit = array(
		'value' => ' Proses ',
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}



function upload_form_validate($pr_yearly_income_id, $komponen_id){
	$errsubmit = false;
	$err = array();
	
	$exts = array('csv','xls');
	$ext = end(explode("." , strtolower( $_FILES['file']['name'] ) ) );
	if( ! is_file($_FILES['file']['tmp_name']) ){
		$errsubmit = true;
		$err[] = "Tidak ada file yang disertakan";
	}
	elseif( ! in_array($ext , $exts)  ){
		$errsubmit = true;
		$err[] = "File tidak dikenali";
	}	
	
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}


function upload_form_page_submit($pr_yearly_income_id, $komponen_id){
	$dest ='files/csv/file_'.date('Ymd_his').'.csv';
	$ori = $_FILES['file']['tmp_name'];
	$upload = move_uploaded_file($ori , $dest); 
	if($upload){
		my_query("TRUNCATE TABLE `pr_yearly_income_temp` ");	
		$handle = fopen($dest, "r"); 
		while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
			if(trim($data[0])<>'' && $data[1]<> ''){
				$karyawan_id =check_karyawan_by_nik(sprintf('%07d',$data[0]));
				stor_upload_table($karyawan_id,$data[0],$data[1]);
			}
		}
		fclose($handle);
	}
	return $dest ;
}


function stor_upload_table($karyawan_id,$nik,$nominal){
	$datas = array(
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nik'=>my_type_data_str($nik),
		'nominal'=>my_type_data_str($nominal),
	);
	my_insert_record('pr_yearly_income_temp',$datas);
}


function display_upload($pr_yearly_income_id, $komponen_id){
	$row = array(); 
	$header = array(
		'NIK'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'Nominal'=>array('style'=>'text-align:right;border-bottom:2px solid;width:70%'),		
	);
	
	$query = "SELECT * FROM pr_yearly_income_temp";
	$result = my_query($query);
	while($ey = my_fetch_array($result)){

		if($ey['karyawan_id'] > 0){	
			$row[] = array(
				'nik'=>$ey['nik'],
				'nominal'=>position_text_align( rp_format($ey['nominal']), 'right'),  
			);
		}else{
			$row[] = array(
				'nik'=>'<font color="red">'.$ey['nik']."</font>",
				'nominal'=>'<font color="red">'.position_text_align( rp_format($ey['nominal']), 'right')."</font>",
				'operasi'=> position_text_align(  $delete_button , 'right'),  
			);
		}
	}
	$datas = table_rows($row);
	return table_builder($header , $datas ,  4 , false ).'<form method="POST"><input type="submit" value=" Upload" class="main_button"/></form>'; 
}


function form_upload_create($pr_yearly_income_id, $komponen_id, $karyawan_id){
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js' ,
			'components/system/js/calendar/calendarDateInput.js'
		)
	);
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
		)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
	');

	$view = form_header( "upload" , "upload"  );

	$query = "
			SELECT * FROM pr_yearly_income_upload
			WHERE pr_yearly_income_id={$pr_yearly_income_id} 
			AND komponen_id={$komponen_id} 
			AND karyawan_id={$karyawan_id} 
		";
	$result = my_query($query );
	$fields=my_fetch_array($result) ;
	$karyawan = loaddata_karyawan($fields['karyawan_id']);
	$nama_karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>( isset($_POST['nama_karyawan'] ) ? $_POST['nama_karyawan'] : $karyawan['nik'].($fields ? '/' : '').$karyawan['nama']  ),
		'id'=>'nama_karyawan' , 
		'type'=>'text',
		'size'=>'45' ,
		'onchange'=>'javascript:checkName(this.value)'
	);
	
	$form_field_nama_karyawan = form_dynamic($nama_karyawan);
	$view .= form_field_display( get_komponen($pr_yearly_income_id,$komponen_id)   , "Nama Komponen Gaji"    );
	$view .= form_field_display( $form_field_nama_karyawan  , "NIK/Nama Karyawan"    );
	
	$nominal = array(
			'name'=>'nominal',
			'value'=>(isset($_POST['nominal'])? $_POST['nominal'] : $fields['nominal']),
			'id'=>'nominal' 
	);
	$form_nominal = form_money($nominal);
	$view .= form_field_display( $form_nominal , "Nominal" );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function edit_upload_validate($pr_yearly_income_id, $komponen_id, $karyawan_id){
	$errsubmit = false;
	$err = array();
	$nominal = set_text_as_number($_POST['nominal']);
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);
	
	if(! $karyawan_id){
		$errsubmit = true;
		$err[] = "Karyawan tidak dikenal";
	}
	
	if( ! is_numeric($nominal) OR $nominal == 0 ){
		$errsubmit = true;
		$err[] = "Karakter nominal bukan angka";
	}	
	
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function submit_edit_upload($pr_yearly_income_id, $komponen_id, $id){
	$nominal = set_text_as_number($_POST['nominal']);
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);
	if($id > 0){
		$query = "
			UPDATE pr_yearly_income_upload SET nominal='".$nominal."',   
			karyawan_id={$karyawan_id} 
 			WHERE pr_yearly_income_id={$pr_yearly_income_id} 
			AND komponen_id={$komponen_id} 
			AND karyawan_id={$id} 
		";
		return my_query($query );
	}	
	
	$datas = array(
		'pr_yearly_income_id' 	=> my_type_data_int($pr_yearly_income_id),		 	 	 	 	 	 	
		'komponen_id' 	=> my_type_data_int($komponen_id),		 	 	 	 	 	 	
		'karyawan_id' 	=> my_type_data_int($karyawan_id),	 	 	 	 	 	 	
		'nominal'		=> my_type_data_str($_POST['nominal']),
	);
	return my_insert_record('pr_yearly_income_upload' , $datas);
}


function set_takehome_pay($karyawan_id , $pr_yearly_income_id){
	 
	$query1 = "SELECT a.nominal  FROM pr_yearly_income_karyawan  a
			INNER JOIN pr_yearly_income_komponen b ON a.komponen_id = b.komponen_id
			WHERE a.karyawan_id = {$karyawan_id} 
			AND a.pr_yearly_income_id	= {$pr_yearly_income_id} 
			AND   b.kategori = '-' AND  b.model_entry = 0  ";
	$result1 = my_query($query1);
	$row1 = my_fetch_array($result1);
	
	$query2 = "SELECT a.nominal  FROM pr_yearly_income_karyawan  a
			INNER JOIN pr_yearly_income_komponen b ON a.komponen_id = b.komponen_id
			WHERE a.karyawan_id = {$karyawan_id} 
			AND a.pr_yearly_income_id	= {$pr_yearly_income_id} 
			AND    b.model_entry = 1  ";
	$result2 = my_query($query2);
	$row2 = my_fetch_array($result2);
	
	
	$takehomepay =  $row1['nominal'] + $row2['nominal'];
	//BAITUL MALL
	$pembulatan = ($takehomepay-floor($takehomepay/FAKTOR_KALI_PEMBULATAN)*FAKTOR_KALI_PEMBULATAN)*-1 ;
	$query_check_pembulatan = "SELECT komponen_id FROM pr_yearly_income_komponen
		WHERE pr_yearly_income_id = {$pr_yearly_income_id} 
		AND model_entry = 5 ";
	$result_rpw = my_query($query_check_pembulatan);
	$row_rpw = my_fetch_array($result_rpw);
	
	my_query("UPDATE pr_yearly_income_karyawan SET nominal = '{$pembulatan}' 
		WHERE komponen_id = {$row_rpw['komponen_id']} AND karyawan_id = {$karyawan_id} ");
	$datas1 = array(
			'pr_yearly_income_id'=>my_type_data_int($pr_yearly_income_id),
			'karyawan_id'=>my_type_data_int($karyawan_id),
			'komponen_id'=>my_type_data_int($row_rpw['komponen_id'] ),
			'nominal'=>my_type_data_str( $pembulatan ),
		);
	//	my_insert_record('pr_yearly_income_karyawan',$datas1);
		
		
	$query_check_takehomepay = "SELECT komponen_id FROM pr_yearly_income_komponen
		WHERE pr_yearly_income_id = {$pr_yearly_income_id} 
		AND model_entry = 6 ";
	$result_rp = my_query($query_check_takehomepay);
	$row_rp = my_fetch_array($result_rp);
	$thp = $takehomepay + $pembulatan;
	my_query("UPDATE pr_yearly_income_karyawan SET nominal = '{$thp}' 
		WHERE komponen_id = {$row_rp['komponen_id']} AND karyawan_id = {$karyawan_id} ");

	$datas = array(
			'pr_yearly_income_id'=>my_type_data_int($pr_yearly_income_id),
			'karyawan_id'=>my_type_data_int($karyawan_id),
			'komponen_id'=>my_type_data_int($komponen_id ),
			'nominal'=>my_type_data_str( $takehomepay ),
		);
		  
		//my_insert_record('pr_yearly_income_karyawan',$datas1);
}	
	
	
function get_komponen($pr_yearly_income_id,$komponen_id){

	$query = "
			SELECT label  		
			FROM pr_yearly_income_komponen
			WHERE pr_yearly_income_id={$pr_yearly_income_id} 
			AND komponen_id={$komponen_id} 
			LIMIT 1
		";
	$result = my_query($query );
	$results=my_fetch_array($result) ;
	return $results['label'] ;
}	
	
	
	
function list_exception($pr_yearly_income_id, $komponen_id){
	my_set_code_js('

		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=exception_delete&id=\'+id;
			}
			return false;
		}
	');
	$header = array(
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Nama'=>array('style'=>'text-align:left;border-bottom:2px solid;width:65%'),  
		'Nominal'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	if(isset($_GET['key']))
		$query = "SELECT a.yearly_income_exception_id, a.karyawan_id , a.nominal FROM pr_yearly_income_exception a
			INNER JOIN karyawan c  ON a.karyawan_id = c.karyawan_id
			INNER JOIN biodata d ON d.biodata_id = c.biodata_id
			WHERE a.pr_yearly_income_id={ $pr_yearly_income_id } 
			AND a.komponen_id={ $komponen_id } 
			AND ( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' )
			ORDER BY c.NIK
			";
	else
		$query = " SELECT a.yearly_income_exception_id, a.karyawan_id , a.nominal FROM pr_yearly_income_exception a
			INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id 
			WHERE a.pr_yearly_income_id=".$pr_yearly_income_id."
			AND a.komponen_id=".$komponen_id 
		;
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =7;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		  
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&pr_yearly_income_id={$_GET['pr_yearly_income_id']}&komponen_id={$_GET['komponen_id']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$karyawan = loaddata_karyawan($ey['karyawan_id']);

		$editproperty = array(
			'href'=>'index.php?com=pr_yearly_income&task=create_exception
				&pr_yearly_income_id='.$pr_yearly_income_id.'
				&komponen_id='.$komponen_id.'
				&id='.$ey['yearly_income_exception_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		 
		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['yearly_income_exception_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		
		$row[] = array(
			'nik'=>position_text_align ($karyawan['nik'] ,'center'),    
			'nama'=> $karyawan['nama_gelar'] ,      
			'nominal'=>position_text_align(rp_format($ey['nominal']) , 'right')  ,
			'operasi'=> position_text_align( $edit_button .' '.$delete_button, 'right'),
		); 
	}
	$datas = table_rows($row);
	$paging = $kgPagerOBJ ->showPaging();	

	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .='<table width="100%"   style="border-collapse:collapse;border-color:white" cellspacing="0" cellpadding="2">';
	$view .= form_field_display( '<span class="label_form"><b>'.get_komponen($pr_yearly_income_id,$komponen_id).'</b></span>', "<b>Komponen</b>"    ); 
	$view .= '<tr><td><br/>'.$search .'</td><td valign="bottom"><div style="width:100%;text-align:right;">
	<input type="button" value="Tambah Data" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com=pr_yearly_income&task=create_exception&pr_yearly_income_id='.$pr_yearly_income_id.'&komponen_id='.$komponen_id.'&id=0\' });" class="main_button" /> 
	<input type="button" value="Kembali" class="main_button" onclick="javascript:location.href=\'index.php?com=pr_yearly_income&task=list_komponen&pr_yearly_income_id='.$pr_yearly_income_id.'\'"/></div></td></tr>'   ; 
	return  $view.   table_builder($header , $datas ,  6 , false ,$paging );	
}

function form_yearly_exception($pr_yearly_income_id, $komponen_id, $id=0){
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js' ,
			'components/system/js/calendar/calendarDateInput.js'
		)
	);
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
		)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
	');

	$view = form_header( "upload" , "upload"  );

	$fields=my_get_data_by_id('pr_yearly_income_exception','yearly_income_exception_id',$id);
	$karyawan = loaddata_karyawan($fields['karyawan_id']);
	$nama_karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>( isset($_POST['nama_karyawan'] ) ? $_POST['nama_karyawan'] : $karyawan['nik'].($fields ? '/' : '').$karyawan['nama']  ),
		'id'=>'nama_karyawan' , 
		'type'=>'text',
		'size'=>'45' ,
		'onchange'=>'javascript:checkName(this.value)'
	);
	
	$form_field_nama_karyawan = form_dynamic($nama_karyawan);
	$view .= form_field_display( get_komponen($pr_yearly_income_id,$komponen_id)   , "Nama Komponen"    );
	$view .= form_field_display( $form_field_nama_karyawan  , "NIK/Nama Karyawan"    );
	
	$nominal = array(
			'name'=>'nominal',
			'value'=>(isset($_POST['nominal'])? $_POST['nominal'] : rp_format($fields['nominal'])),
			'id'=>'nominal' 
	);
	$form_nominal = form_money($nominal);
	$view .= form_field_display( $form_nominal , "Nominal" );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function exception_validate($pr_yearly_income_id, $komponen_id, $id){
	$errsubmit = false;
	$err = array();
	$nominal = set_text_as_number($_POST['nominal']);
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);
	
	if(! $karyawan_id){
		$errsubmit = true;
		$err[] = "Karyawan tidak dikenal";
	}
	
	if( ! is_numeric($nominal) OR $nominal == 0 ){
		$errsubmit = true;
		$err[] = "Karakter nominal bukan angka";
	}	
	
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function exception_submit($pr_yearly_income_id, $komponen_id, $id){
	$nominal = set_text_as_number($_POST['nominal']);
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);

	$datas = array(
		'pr_yearly_income_id' 	=> my_type_data_int($pr_yearly_income_id),		 	 	 	 	 	 	
		'komponen_id' 	=> my_type_data_int($komponen_id),		 	 	 	 	 	 	
		'karyawan_id' 	=> my_type_data_int($karyawan_id),	 	 	 	 	 	 	
		'nominal'		=> my_type_data_str($nominal),
	);

	
	if($id > 0){
		return my_update_record('pr_yearly_income_exception' , 'yearly_income_exception_id' , $id , $datas);
	}	
	return my_insert_record('pr_yearly_income_exception' , $datas);
}

function get_pajak( $komponen_pajak1, $komponen_pajak2, $karyawan_id, $periode_id, $pr_yearly_income_id  ){
	return 0;
	$query = "
			SELECT *  		
			FROM pr_gaji_pajak
			WHERE karyawan_id={$karyawan_id} 
			AND periode_id={$periode_id} 
			LIMIT 1
		";
	$result = my_query($query );
	if(my_num_rows($result) == 0){
		return 0;
	}	
	$ey=my_fetch_array($result) ;
	
	$komponen_pajak = array();
	$komponen_pajak[1] = $ey['pajak_01']+$komponen_pajak1[1]+$komponen_pajak2[1] ;
	$komponen_pajak[2]= 0 ;
	$komponen_pajak[3]= $ey['pajak_03']+$komponen_pajak1[3]+$komponen_pajak2[3] ;
	$komponen_pajak[4]= $ey['pajak_04']+$komponen_pajak1[4]+$komponen_pajak2[4] ;
	$komponen_pajak[5]= $ey['pajak_05'] +$komponen_pajak1[5]+$komponen_pajak2[5] ;
	$komponen_pajak[6]= $ey['pajak_06'] +$komponen_pajak1[6]+$komponen_pajak2[6] ;
	$komponen_pajak[7]= $komponen_pajak[1]
		+$komponen_pajak[2]
		+$komponen_pajak[3]
		+$komponen_pajak[4]
		+$komponen_pajak[5]
		+$komponen_pajak[6] ;	
	$komponen_pajak[8]=$ey['pajak_08']+$komponen_pajak1[8]+$komponen_pajak2[8] ;
	$komponen_pajak[9]=$komponen_pajak[7]
		+$komponen_pajak[8] ;
	//BIAYA JABATAN
	$persen_bj = get_kolom_pajak_hitung(19); 
	$max_bj = get_kolom_pajak_hitung(20)/12; 
	$kal_persen_bj =0.01 * $persen_bj *$komponen_pajak[9] ;
	if( $kal_persen_bj > $max_bj ) 
		$komponen_pajak[10] = $max_bj;
	else  
		$komponen_pajak[10] = $kal_persen_bj; 	
	$komponen_pajak[11]=0 ;
	$komponen_pajak[12]= $ey['pajak_12']+$komponen_pajak1[12]+$komponen_pajak2[12] ;

	$komponen_pajak[13]=$komponen_pajak[10]+$komponen_pajak[11]+$komponen_pajak[12]	;
	$komponen_pajak[14]=$komponen_pajak[9]-$komponen_pajak[13]	;
	$komponen_pajak[15]=0;

	//disetahunkan
	$komponen_pajak[16]=($komponen_pajak[14]-$komponen_pajak[15])*12; 
	
	$query = "
		SELECT value FROM pr_konfigurasi_pajak
		WHERE kolom_id = 16";
	$ptkps = my_fetch_array(my_query($query));
	$ptkp=$ptkps['value'];
	if($karyawan['ptkp_status']=='K'){
		$query = "
			SELECT value FROM pr_konfigurasi_pajak 
			WHERE kolom_id = 17";
		$ptkps = my_fetch_array(my_query($query));
		$ptkp+=$ptkps['value'];
	}
	$query = "
		SELECT value FROM pr_konfigurasi_pajak 
		WHERE kolom_id = 18";
	$ptkps = my_fetch_array(my_query($query));
	$tanggung=MIN($karyawan['ptkp_tanggung'],3);
	$ptkp+=($ptkps['value']*$tanggung);
	$komponen_pajak[17]=$ptkp;
	$komponen_pajak[18]=$komponen_pajak[16]-$komponen_pajak[17];
	//UPDATE PR_GAJI_PAJAK BEFORE TAX	
	$datas = array(
		'pr_yearly_income_id'=>my_type_data_int($pr_yearly_income_id),
		'karyawan_id'=>my_type_data_int($karyawan_id), 
		'pajak_01'	=>my_type_data_str($komponen_pajak[1]), 			
		'pajak_02'	=>my_type_data_str($komponen_pajak[2]), 			
		'pajak_03'	=>my_type_data_str($komponen_pajak[3]), 			
		'pajak_04'	=>my_type_data_str($komponen_pajak[4]), 			
		'pajak_05'	=>my_type_data_str($komponen_pajak[5]), 			
		'pajak_06'	=>my_type_data_str($komponen_pajak[6]), 			
		'pajak_07'	=>my_type_data_str($komponen_pajak[7]), 			
		'pajak_08'	=>my_type_data_str($komponen_pajak[8]), 			
		'pajak_09'	=>my_type_data_str($komponen_pajak[9]), 			
		'pajak_10'	=>my_type_data_str($komponen_pajak[10]), 			
		'pajak_11'	=>my_type_data_str($komponen_pajak[11]), 			
		'pajak_12'	=>my_type_data_str($komponen_pajak[12]), 			
		'pajak_13'	=>my_type_data_str($komponen_pajak[13]), 			
		'pajak_14'	=>my_type_data_str($komponen_pajak[14]), 			
		'pajak_15'	=>my_type_data_str($komponen_pajak[15]), 			
		'pajak_16'	=>my_type_data_str($komponen_pajak[16]), 			
		'pajak_17'	=>my_type_data_str($komponen_pajak[17]), 			
		'pajak_18'	=>my_type_data_str($komponen_pajak[18]), 			
	);
	my_insert_record('pr_yearly_income_pajak' , $datas);

	//HITUNG ULANG

	$pajak=get_tunjangan_pajak_hitung($pr_yearly_income_id,$karyawan_id);
	$komponen_pajak[2]+=$pajak ;
	$komponen_pajak[7]+=$pajak ;
	$komponen_pajak[9]+=$pajak ;
	//BIAYA JABATAN
	$kal_persen_bj =0.01 * $persen_bj *$komponen_pajak[9] ;
	if( $kal_persen_bj > $max_bj ) 
		$komponen_pajak[10] = $max_bj;
	else  
		$komponen_pajak[10] = $kal_persen_bj; 	

	$komponen_pajak[11]=0 ; //sementara masih nol
	$komponen_pajak[13]=$komponen_pajak[10]+$komponen_pajak[11]+$komponen_pajak[12]	;
	$komponen_pajak[14]=$komponen_pajak[9]-$komponen_pajak[13]	;	
	//disetahunkan
	$komponen_pajak[16]=($komponen_pajak[14]-$komponen_pajak[15])*12; 
	$komponen_pajak[18]=$komponen_pajak[16]-$komponen_pajak[17];
	
	$komponen_pajak[19]=get_pph21_hitung($komponen_pajak[18]) ;
	$komponen_pajak[21]=($komponen_pajak[19]-$komponen_pajak[20])/12 ;
	$komponen_pajak[21]=100 * ( floor( $komponen_pajak[21] /100) ) ;
	$komponen_pajak[22]=0 ;
	$komponen_pajak[23]=$komponen_pajak[21]-$komponen_pajak[22] ;

	//UPDATE PR_GAJI_PAJAK AFTER TAX	
	$query = "UPDATE pr_yearly_income_pajak SET 
		pajak_02 = '".$komponen_pajak[2]."' 
		,pajak_07 = '".$komponen_pajak[7]."' 		
		,pajak_09 = '".$komponen_pajak[9]."'
		,pajak_10 = '".$komponen_pajak[10]."'
		,pajak_11 = '".$komponen_pajak[11]."'
		,pajak_13 = '".$komponen_pajak[13]."'
		,pajak_14 = '".$komponen_pajak[14]."'
		,pajak_16 = '".$komponen_pajak[16]."'
		,pajak_18 = '".$komponen_pajak[18]."'
		, pajak_19 = '".$komponen_pajak[19]."' 
		, pajak_20 = '".$komponen_pajak[20]."' 
		, pajak_21 = '".$komponen_pajak[21]."' 
		, pajak_22 = '".$komponen_pajak[22]."' 
		, pajak_23 = '".$komponen_pajak[23]."' 
		WHERE pr_yearly_income_id=".$pr_yearly_income_id." 
		AND karyawan_id=".$karyawan_id; 
	$dres = my_query($query);
	
	$pajak_bonus=$komponen_pajak[23]-$ey['pajak_23'] ;
	return $pajak_bonus ;


} 

function get_tunjangan_pajak_hitung($pr_yearly_income_id,$karyawan_id){

	$query = "SELECT * FROM pr_tarif_pajak
				ORDER BY label_range" ;
	$result=my_query($query);
	$arange_tmp=$arange=$atarif=$apersen=$apenambahan=$amaksimal=array();
	$max_tarif=-1;
	while($ey = my_fetch_array($result)){
		$max_tarif++;
		$atarif[$max_tarif]=$ey['tarif'];
		$arange_tmp[$max_tarif]=(int) $ey['label_range'];
		$apersen[$max_tarif]=$ey['tarif']/(100-$ey['tarif']) ;
		if($max_tarif>0){
			$arange[$max_tarif-1]=$arange_tmp[$max_tarif];
			$apenambahan[$max_tarif]=$atarif[$max_tarif-1]*0.01*
				($arange_tmp[$max_tarif]-$arange_tmp[$max_tarif-1])+$apenambahan[$max_tarif-1] ;
		}else{
			$apenambahan[$max_tarif]=0;
		}
		$amaksimal[$max_tarif]=$arange_tmp[$max_tarif]-$apenambahan[$max_tarif] ;	
		
	}
	//var_dump($apersen);var_dump($apenambahan);var_dump($amaksimal);exit;	
	
	$query = " SELECT * FROM pr_yearly_income_pajak 
			WHERE karyawan_id = {$karyawan_id } 
			AND pr_yearly_income_id={$pr_yearly_income_id } " ;
	$pajaks = my_fetch_array(my_query($query));	
	$tmpData = $pajaks['pajak_18'];
	$tmpData = 100 * ( floor( $tmpData /100) ); 
	foreach($amaksimal as $key=>$value){
		if($tmpData >= $amaksimal[$key]){
			$nrange=$key;
		}
	}
	$tpj = ( $tmpData - $amaksimal[$nrange] ) * $apersen[$nrange] + $apenambahan[$nrange] ;
	unset($tmpData);
	$tpj = $tpj /12 ;
	$tpj = 100 * ( floor( $tpj /100) ); 	
	
//	Recalculation	

	$gross = 0;
	$persen_bj = get_kolom_pajak_hitung(19); 
	$max_bj = get_kolom_pajak_hitung(20)/12; 

	for( $y = 0; $y<=100; $y++){ 
		$tmpData = $pajaks['pajak_09'] + $tpj;
		 
		 if( ( $persen_bj * $tmpData * 0.01 ) > ( $max_bj  ) ){
			$tmpData -=  $max_bj;
		 }else{
			$tmpData -=  $persen_bj * $tmpData * 0.01;
		 }
	
		$tmpData -= $pajaks['pajak_12'];
		$tmpData=$tmpData *12 ; //disetahunkan
		$tmpData -= $pajaks['pajak_17']; //disetahunkan 	
		$tmpData = 100 * floor( $tmpData / 100 );
		$outstanding = $tmpData;
		$pph=0;
		 
		 //TARIF
 
		foreach($arange as $key=>$value){
			$outs=MIN($outstanding,$value);
			$pph +=$outs*$atarif[$key]* 0.01 ;
			$outstanding-=$outs;
		}		 
		 
		 if( $outstanding > 0){
			$pph +=$outs*$atarif[$max_tarif]* 0.01 ;
		 }
		unset($tmpData);
		$pph = $pph/12; //per bulan
		$pph = 100 * ( floor( $pph /100) ); 	
		$tmpData = $pph -  $tpj; //per bulan
		 
		 if($tmpData  == 0){
			break;
		 }
		 
		 if( $y == 0 ){ 
			$gross = $tmpData;
		 }
		 
		else{ 
			 if( ( $gross > 0 AND  $tmpData < 0 ) OR ( $gross < 0  AND $tmpData > 0 ) ){
				break;
			 }
		 }
		
		 if( $gross > 0 ){
			$tpj +=  100;
		 }else{
			$tpj -=  100;
			
		}
			  
		//ENDFOR
		
	} 
	return $tpj;
}

function get_kolom_pajak_hitung($id){
	$query = "
		SELECT value FROM pr_konfigurasi_pajak 
		WHERE kolom_id = {$id} ";
	$res = my_query($query);
	$rw = my_fetch_array($res); 
	return $rw['value'];
}

function get_pph21_hitung($pkp){	
	$query = "SELECT * FROM pr_tarif_pajak
				ORDER BY label_range" ;
	$result=my_query($query);
	$i=$pph=$tarif=$outs=0;
	$outstanding=$pkp;
	while($ey = my_fetch_array($result)){
		if($i>0){
			$outs=MIN($outstanding,(int) $ey['label_range']);
			$pph +=$outs*$tarif* 0.01 ;
			$outstanding-=$outs;
		}
		$i++;
		$tarif=$ey['tarif'] ;
	}	
	if( $outstanding > 0){
		$pph +=$outs*$tarif* 0.01 ;
	}
	return $pph ;
}


function list_karyawan_pajak($pr_yearly_income_id,$karyawan_id){
	$pr_yearly_incomes=my_get_data_by_id('pr_yearly_income','pr_yearly_income_id',$pr_yearly_income_id) ;
	$karyawan = loaddata_karyawan($karyawan_id);
	$periode_id=$pr_yearly_incomes['periode_id'] ;
	$query = " SELECT * FROM pr_gaji_pajak 
		WHERE periode_id=".$periode_id." 
		AND karyawan_id=".$karyawan_id
	;
	$result = my_query($query);	
	$gaji_pajaks = my_fetch_array($result);	

	$query = " SELECT * FROM pr_yearly_income_pajak 
		WHERE pr_yearly_income_id=".$pr_yearly_income_id." 
		AND karyawan_id=".$karyawan_id
	;
	$result = my_query($query);	
	$yearly_pajaks = my_fetch_array($result);	
		
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nama Komponen Pajak'=>array('style'=>'text-align:left;border-bottom:2px solid;width:35%'),  
		'Gaji & Yearly Income (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:30%'),  
		'Gaji (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:30%'),  
	);
	

	$query = "SELECT * FROM pr_komponen_pajak
			ORDER BY komponen_pajak_id"
	;	
	$result = my_query($query);
	$row = array();
	$i=0;

	while($ey = my_fetch_array($result)){
		$i++;

		$pajak_yearly=$yearly_pajaks['pajak_'.sprintf('%02d' ,$i)];
		$pajak_gaji=$gaji_pajaks['pajak_'.sprintf('%02d' ,$i)];
		$row[] = array(
			'e'=>position_text_align($i,'center'),
			'nama'=>$ey['label_pajak'] ,
			'yearly'=> position_text_align(rp_format($pajak_yearly), 'right' ),
			'gaji'=> position_text_align(rp_format($pajak_gaji), 'right' ),
		);
	}
	$pajak_yearly=$yearly_pajaks['pajak_'.sprintf('%02d' ,23)];
	$pajak_gaji=$gaji_pajaks['pajak_'.sprintf('%02d' ,23)];
	$row[] = array(
		'e'=>' ',
		'nama'=>'Pajak Yearly Income' ,
		'yearly'=> ' ',
		'gaji'=> position_text_align(rp_format($pajak_yearly-$pajak_gaji), 'right' ),
	);	

	$datas = table_rows($row); 
	$yearly_incomes = my_get_data_by_id('yearly_income','yearly_income_id',$pr_yearly_incomes['yearly_income_id'] );
	$newline=array(
		'Yearly Income'=>$yearly_incomes['label'],
		'Periode'=>date( "m-Y" , strtotime($pr_yearly_incomes['tanggal']))
	);		
	$pro =   karyawan_header_detail($karyawan,true, $newline  );
	
	$menuontop = array(  
		'Yearly Income' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_yearly_income&task=list_karyawan_komponen&pr_yearly_income_id='.$pr_yearly_income_id.'&karyawan_id='.$karyawan_id.'\''), 
		'Kembali' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_yearly_income&task=list_karyawan&pr_yearly_income_id='.$pr_yearly_income_id.'\''), 
	);
	$printmenu = additional_menu_on_list( $menuontop , '');
	return  $pro.$printmenu.table_builder($header , $datas ,  6 , false   );
}